package com.gtw.modules.sys.controller;

import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbDividendsService;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.TbDataInfo;
import com.gtw.modules.entity.TbUser;
import com.gtw.modules.sys.service.SysDataInfoService;
import com.gtw.modules.sys.service.SysTbUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/** 提现次数
 * Created by wuye on 2018/4/17.
 */
@RestController
@RequestMapping("/sys/datainfo")
public class SysDataInfoController extends AbstractController{
    @Autowired
    private SysDataInfoService dataInfoService;

    @Autowired
    private TbUserService tbUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:datainfo:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<TbDataInfo> dataInfoList = dataInfoService.queryList(query);
        int total = dataInfoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(dataInfoList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:datainfo:info")
    public R info(@PathVariable("id") Integer id){
        TbDataInfo dataInfo = dataInfoService.queryObject(id);

        return R.ok().put("dataInfo", dataInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:datainfo:save")
    public R save(@RequestBody TbDataInfo dataInfo){
        dataInfo.setCreatetime(new Date());
        dataInfo.setCreateuser(getUser().getUsername());
        dataInfoService.save(dataInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:datainfo:update")
    public R update(@RequestBody TbDataInfo dataInfo){
        dataInfo.setUpdateuser(getUser().getUsername());
        dataInfo.setUpdatetime(new Date());
        dataInfoService.update(dataInfo);
        //修改所有用户提现次数
        List<TbUser> userList = tbUserService.queryAll();
        for (TbUser user:userList) {
            user.setWithdrawaltimes(dataInfo.getDate());
            tbUserService.update(user);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:datainfo:delete")
    public R delete(@RequestBody Integer[] ids){
        dataInfoService.deleteBatch(ids);

        return R.ok();
    }
}
